package com.broad.commerce.code.services.jdbc;

import com.broad.commerce.code.base.AbstractJavaService;
import com.broad.commerce.code.modules.BaseModule;
import com.broad.commerce.code.modules.jdbc.EntityModel;
import com.broad.commerce.common.utils.core.StringUtils;
import com.broad.commerce.web.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-05-10 16:21
 * @description: EntityModelService 创建 Entity 对应DTO Model
 **/
@Service
public class EntityModelService extends AbstractJavaService {

	@Override
	public String createClassCode(BaseModule baseModule) {
		if (!(baseModule instanceof EntityModel)) {
			throw new ServiceException("传入参数类型错误！应为 EntityModel");
		}
		return super.createClassCode(baseModule);
	}

	@Override
	protected void createImportCode(StringBuilder sb, BaseModule baseModule) {
		EntityModel entityModel = (EntityModel) baseModule;
		boolean fieldExists = !CollectionUtils.isEmpty(entityModel.getEntityFields());
		//@off
		sb.append("import com.broad.commerce.jdbc.models.BaseIdDTO;").append("\n")
		  .append("import lombok.Getter;").append("\n")
		  .append("import lombok.Setter;").append("\n")
		  .append("import lombok.ToString;").append("\n")
		  .append("import lombok.Builder;").append("\n")
		  .append("import lombok.NoArgsConstructor;").append("\n");//@on
		if (fieldExists) {
			sb.append("import lombok.AllArgsConstructor;").append("\n");
		}//@off
		sb.append("\n").append("import javax.validation.constraints.NotEmpty;").append("\n");
		entityModel.getEntityFields().forEach(field -> {
			if (sb.indexOf(field.getFieldType().getName()) == -1) {
				sb.append("import ").append(field.getFieldType().getName()).append(";\n");
			}
		});
		sb.append("\n");//@on
	}

	@Override
	protected void createAnnotationCode(StringBuilder sb, BaseModule baseModule) {
		EntityModel entityModel = (EntityModel) baseModule;
		boolean fieldExists = !CollectionUtils.isEmpty(entityModel.getEntityFields());
		//@off
		sb.append("@Getter").append("\n")
		  .append("@Setter").append("\n")
		  .append("@ToString").append("\n")
		  .append("@Builder").append("\n")
		  .append("@NoArgsConstructor").append("\n");//@on
		if (fieldExists) {
			sb.append("@AllArgsConstructor").append("\n");
		}
	}

	@Override
	protected void createClassStartTag(StringBuilder sb, BaseModule baseModule) {
		EntityModel entityModel = (EntityModel) baseModule;
		sb.append("public class ").append(entityModel.getClassName()).append(" extends BaseIdDTO {").append("\n");
	}

	@Override
	protected void createClassContent(StringBuilder sb, BaseModule baseModule) {
		EntityModel entityModel = (EntityModel) baseModule;
		//@off
		entityModel.getEntityFields().forEach(field -> {
			String fieldComment = field.getFieldComment();
			if (StringUtils.isNotEmpty(fieldComment)) {
				sb.append("\t").append("// ").append(fieldComment).append("\n");
			}
			if (field.getRequired()) {
				String comment = field.getFieldName();
				if (StringUtils.isNotEmpty(fieldComment)) {
					int index = fieldComment.indexOf("(");
					if(index == -1){
						index = fieldComment.indexOf(":");
					}
					comment = index != -1 ? fieldComment.substring(0, index) : fieldComment;
				}
				sb.append("\t").append("@NotEmpty(message = \"").append(comment).append("不能为空\")").append("\n");
			}
			sb.append("\t").append("private ").append(field.getFieldType().getSimpleName()).append(" ")
			  .append(field.getFieldName()).append(";\n");
		});//@on
	}
}
